import 'package:flutter_billi/http/dao/login_dao.dart';
import 'package:http/http.dart';

enum HttpMethod { GET, POST, DELETE }

// 基础请求 基类
abstract class BaseRequest {
  var pathParams;
  var useHttps = true;

  /// 返回域名
  String authority() {
    return 'api.devio.org';
  }

  HttpMethod httpMethod();

  // 返回 请求路径
  String path();

  String url() {
    Uri uri;
    var pathStr = path();

    // 拼接path参数
    if (pathParams != null) {
      if (path().endsWith("/")) {
        pathStr = "${path()}$pathParams";
      } else {
        pathStr = "${path()}/$pathParams";
      }
    }

    if (needLogin()) {
      addHeader(LoginDao.BOARDING_PASS, LoginDao.getBoardingPass());
    }
    // http 和 https 切换
    if (useHttps) {
      uri = Uri.https(authority(), pathStr, params);
    } else {
      uri = Uri.http(authority(), pathStr, params);
    }
    print("uri:${uri.toString()}");
    return uri.toString();
  }

  // 是否需要登陆
  bool needLogin();
  Map<String, String> params = Map();
  BaseRequest add(String s, Object o) {
    params[s] = o.toString();
    return this;
  }

  // 请求是否需要鉴权
  // 添加header
  // Map<String, String> header = Map();
  Map<String, dynamic> header = {
    'course-flag': 'fa',
    'auth-token': 'ZmEtMjAyMS0wNC0xMiAyMToyMjoyMC1mYQ==fa',
  };

  BaseRequest addHeader(String s, Object o) {
    header[s] = o.toString();
    return this;
  }
}
